define(["jquery", "easy-admin", "treetable", "iconPickerFa", "autocomplete"], function ($, ea) {

    var table = layui.table,
        treetable = layui.treetable,
        iconPickerFa = layui.iconPickerFa,
        autocomplete = layui.autocomplete;

    var init = {
        table_elem: '#currentTable',
        table_render_id: 'currentTableRenderId',
        index_url: 'system.myapp/index',
        add_url: 'system.myapp/add',
        delete_url: 'system.myapp/delete',
        edit_url: 'system.myapp/edit',
        modify_url: 'system.myapp/modify',
        crud_url:'system.myapp/crud',
        updateFile:'system.myapp/updateField',
    };

    var Controller = {
        index: function () {

            var util = layui.util;
            ea.table.render({
                init: init,
                toolbar: ['refresh','add'],
                page:false,
                cols: [[
                    // {field: 'id', width: 80, title: 'ID', search: false},
                    {field: 'title', title: '插件名', search: true},
                    {field: 'name', title: '标识', search: true},
                    {field: 'intro',  title: '描述', search: false},
                    {field: 'author',title: '作者', search: false},
                    {field: 'version',title: '版本号', search: false},
                    {field: 'relyon',  title: '依赖模块', search: false},
                    {field: 'state', title: '是否安装', width: 185, search: 'select', selectList: {0: '禁用', 1: '启用'}, tip: '开启|关闭', templet: ea.table.switch},
                    // {field: 'state', title: '状态', width: 85, search: false,selectList:{0:"未安装",1:"已安装"}, tip: "未安装|已安装",filter: 'status', templet: ea.table.switch},
                    {
                        width: 150,
                        title: '操作',
                        // templet: ea.table.tool,
                        templet: '#right_button',
                        // operat: [
                        //     [{
                        //         text: '添加下级',
                        //         url: init.add_url,
                        //         method: 'open',
                        //         auth: 'add',
                        //         class: 'layui-btn layui-btn-xs layui-btn-normal',
                        //         extend: '',
                        //     },
                        //         {
                        //             text: '编辑',
                        //             url: init.edit_url,
                        //             method: 'open',
                        //             auth: 'edit',
                        //             class: 'layui-btn layui-btn-xs layui-btn-success',
                        //             extend: '',
                        //         },
                        //         {
                        //             text: '一键curd',
                        //             url: init.crud_url,
                        //             // method: 'ajax',
                        //             method: 'open',
                        //             auth: 'curd',
                        //             class: 'layui-btn layui-btn-xs layui-btn-success',
                        //             // extend: 'data-full="true"',
                        //         },
                        //         // {
                        //         //     text: '更新字段文件',
                        //         //     url: init.updateFile,
                        //         //     method: 'ajax',
                        //         //     auth: 'curd',
                        //         //     class: 'layui-btn layui-btn-xs layui-btn-success',
                        //         //     extend: 'data-full="true"',
                        //         // }
                        //     ],
                        //     'delete'
                        // ]
                    }
                ]],
            });

            ea.listen();
        },
        add: function () {
            iconPickerFa.render({
                elem: '#icon',
                url: PATH_CONFIG.iconLess,
                limit: 12,
                click: function (data) {
                    $('#icon').val('fa ' + data.icon);
                },
                success: function (d) {
                    console.log(d);
                }
            });
            autocomplete.render({
                elem: $('#href')[0],
                url: ea.url('system.menu/getMenuTips'),
                template_val: '{{d.node}}',
                template_txt: '{{d.node}} <span class=\'layui-badge layui-bg-gray\'>{{d.title}}</span>',
                onselect: function (resp) {
                }
            });

            ea.listen(function (data) {
                return data;
            }, function (res) {
                ea.msg.success(res.msg, function () {
                    var index = parent.layer.getFrameIndex(window.name);
                    parent.layer.close(index);
                    parent.$('[data-treetable-refresh]').trigger("click");
                });
            });
        },
        edit: function () {
            iconPickerFa.render({
                elem: '#icon',
                url: PATH_CONFIG.iconLess,
                limit: 12,
                click: function (data) {
                    $('#icon').val('fa ' + data.icon);
                },
                success: function (d) {
                    console.log(d);
                }
            });
            autocomplete.render({
                elem: $('#href')[0],
                url: ea.url('system.menu/getMenuTips'),
                template_val: '{{d.node}}',
                template_txt: '{{d.node}} <span class=\'layui-badge layui-bg-gray\'>{{d.title}}</span>',
                onselect: function (resp) {
                }
            });

            ea.listen(function (data) {
                return data;
            }, function (res) {
                ea.msg.success(res.msg, function () {
                    var index = parent.layer.getFrameIndex(window.name);
                    parent.layer.close(index);
                    parent.$('[data-treetable-refresh]').trigger("click");
                });
            });
        },
        crud:function (){
            ea.table.listenSwitch({filter: 'status', url: init.modify_url});

            ea.table.listenEdit(init, 'currentTable', init.table_render_id, true);

            ea.listen();
        }
    };
    return Controller;
});